package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.example.reggie.dto.OrdersDto;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Orders;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.entity.ShoppingCart;
import com.example.reggie.service.OrdersService;
import com.example.reggie.service.ShoppingCartService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;


    /**
     * 提交订单
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders order){
        log.info(order.toString());
        ordersService.submit(order);

        return R.success("下单成功");
    }

    /**
     * 分页展示订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize){
        log.info("page ={},pageSize={},name={}",page,pageSize);

        Page<OrdersDto> ordersDtoPage=ordersService.getPage(page,pageSize);

        return R.success(ordersDtoPage);
    }

    /**
     * 再来一单 功能实现
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders order){
        ordersService.again(order);

        return R.success("订单信息添加成功！");
    }


    /*接下来是管理端的方法*/
    /**
     * 在管理端分页展示订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> Mpage(int page, int pageSize){
        log.info("page ={},pageSize={},name={}",page,pageSize);

        Page<OrdersDto> ordersDtoPage=ordersService.getPage(page,pageSize);

        return R.success(ordersDtoPage);
    }

    /**
     * 修改订单状态信息
     * @param order
     * @return
     */
    @PutMapping
    public R<String> UpdateStatus(@RequestBody Orders order){
        Orders one = ordersService.getById(order.getId());
        one.setStatus(order.getStatus());
        ordersService.updateById(one);
        return R.success("订单状态信息修改成功");
    }

}
